<?php
require_once 'CMS/Util.php';
require_once 'CMS/User.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseAdminController.php';
require_once 'CMS/AdminCommon.php';
//Người tạo : Lại Ngọc Thái Châu - Team 0
//Module 	: homepage admin
//Chức năng	: show thong ke

class Admin_IndexController extends BaseAdminController {
		
	public function init(){
		parent::init();
		$this->_helper->layout()->setLayout('layoutadmin');
		$user = new User();
		$user->init();
		$check = $user->check_login();
		if($check ==true)
			$user->permission();
	}
	
	public function indexAction()
	{
		//Start ------- Truyền Parameter qua View
		$this->view->actionUrl = "/admin/index";
		//End ------- Truyền Parameter qua View
		//session_destroy();
		
		//Start------Khai báo các parameter
		$dbUser = new Admin_Model_Users();
		$dbBill = new Admin_Model_Bill();
		$dbThing = new Admin_Model_Things();
		$dbComment = new Admin_Model_Comment();
		$dateInt = new Util();
		//End--------Khai báo các parameter
		
		//Start------Get database
		$limit = 10;
		$latestorder 	= $dbBill->getLastestOrder($limit);
		$allorder 		= $dbBill->getAllOrders();
		$allcustomer 	= $dbUser->selectUserByGroup(2);
		$allproduct 	= $dbThing->getAllThingsbyType(1);
		$allcomment 	= $dbComment->getAllComment();
		$allsales 		= $dbBill->getOrderByStatus(2);
		//End--------Get database
		
		//Start------Get list latest 10 order
		$i=0;
		if(isset($latestorder) && $latestorder!=null){
			foreach($latestorder as $order)
			{
				$orderlist[$i]=array(
					"bill_id"			=>$order["bill_id"],
					"full_name"			=>$order["full_name"],
					"status"			=>$order["status_name"],
					"created_date" 		=>$dateInt->makeDays($order['created_date']),
					"total_bill_price"	=>$order["total_bill_price"],
				);
				$i++;
			}
		}
		//End--------Get list latest 10 order
		
		//Start------Get total sales
		$totalsales = 0;
		$totalsalesyear = 0;
		if(isset($allsales) && $allsales!=null){
			foreach($allsales as $sale)
			{
				$totalsales += $sale['total_bill_price'];
				$year = gmdate('Y', $sale['created_date']);
				$now = $dateInt->get_time_now();
				$yearnow = gmdate('Y', $now);
				if($year == $yearnow)
				{
					$totalsalesyear += $sale['total_bill_price'];
				}
			}
		}
		//End--------Get total sales
		
		//Start ------- Truyền Parameter qua View
		$this->view->orderlist = $orderlist;
		$this->view->countorder = count($allorder);
		$this->view->countcustomer = count($allcustomer);
		$this->view->countproduct = count($allproduct);
		$this->view->countcomment = count($allcomment);
		$this->view->totalsales = $totalsales;
		$this->view->totalsalesyear = $totalsalesyear;
		//End ------- Truyền Parameter qua View
	}
}